package xyz.icxl.kotlion.ext.binding

import android.text.Editable
import android.view.KeyEvent
import android.widget.EditText
import androidx.core.widget.doAfterTextChanged
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.KeyboardUtils
import xyz.icxl.kotlion.ext.view.selfClickListener


/**
 * 点击回车监听
 * @param l 点击后会回调该函数
 */
@BindingAdapter("ext_onClickEnterListener")
fun EditText.onClickEnterListener(l:()->Unit = {}){
    setOnKeyListener { v, keyCode, event ->
        if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN){
            KeyboardUtils.hideSoftInput(v)
            l()
        }
        false
    }
}



@BindingAdapter("ext_textChangeAfter")
fun EditText.textChangeAfter(f:(Editable?)->Unit = {}){
    addTextChangedListener(doAfterTextChanged(f))
}

/**
 * 获取Int值的text
 */
fun EditText.getIntText():Int?{
    return if (text.isNullOrEmpty())
        null
    else{
        try {
            text.toString().toInt()
        }catch (e:Exception){
            null
        }
    }
}